package com.warehouse.module.business.service.cargoinfo;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.warehouse.framework.mybatis.core.util.MyBatisUtils;
import com.warehouse.framework.security.core.util.SecurityFrameworkUtils;
import com.warehouse.module.business.controller.admin.cargoplan.vo.CancelPlanVO;
import com.warehouse.module.business.dal.dataobject.cargoRecord.CargoRecordDO;
import com.warehouse.module.business.dal.dataobject.cargoplan.CargoPlanDO;
import com.warehouse.module.business.dal.mysql.cargoRecord.CargoRecordMapper;
import com.warehouse.module.business.enums.OrderStatusEnum;
import com.warehouse.module.business.enums.PackStatusEnum;
import com.warehouse.module.business.enums.PlanStatusEnum;
import com.warehouse.module.business.service.cargofile.CargoFileService;
import com.warehouse.module.business.service.cargoplan.CargoPlanService;
import com.warehouse.module.business.utils.ValidationRulesUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

import com.warehouse.module.business.controller.admin.cargoinfo.vo.*;
import com.warehouse.module.business.dal.dataobject.cargoinfo.CargoInfoDO;
import com.warehouse.framework.common.pojo.PageResult;
import com.warehouse.framework.common.pojo.PageParam;
import com.warehouse.framework.common.util.object.BeanUtils;

import com.warehouse.module.business.dal.mysql.cargoinfo.CargoInfoMapper;

import static com.warehouse.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.warehouse.module.business.enums.ErrorCodeConstants.*;

/**
 * 货物资料 Service 实现类
 *
 * @author Eric
 */
@Service
@Validated
public class CargoInfoServiceImpl implements CargoInfoService {

    @Resource
    private CargoInfoMapper cargoInfoMapper;
    @Resource
    private CargoFileService cargoFileService;
    @Resource
    private CargoRecordMapper cargoRecordMapper;
    @Resource
    private CargoPlanService cargoPlanService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long createCargoInfo(CargoInfoSaveReqVO createReqVO) {
        // 插入
        CargoInfoDO cargoInfo = BeanUtils.toBean(createReqVO, CargoInfoDO.class);
        if(!ValidationRulesUtils.checkTrackingNumber(createReqVO.getMainOrderId())){
            throw exception(CARGO_INFO_ORDER_ERROR);
        }
        if(!checkOrderUnique(createReqVO.getMainOrderId(), null)){
            throw exception(CARGO_INFO_ORDER_NO_ERROR);
        }
        cargoInfo.setOrderId(ValidationRulesUtils.createOrderId());
        if(cargoInfo.getStorage() == 1) {
            cargoInfo.setStatus(OrderStatusEnum.PENDING_ENTRY.getStatus());
            cargoInfo.setPlanStatus(PlanStatusEnum.NOTHING.getStatus());
        }else{
            cargoInfo.setStatus(OrderStatusEnum.PENDING_CHECK.getStatus());
            cargoInfo.setPlanStatus(PlanStatusEnum.PENDING_PLANING.getStatus());
        }
//        cargoInfo.setStatus(OrderStatusEnum.WAITING.getStatus());
        cargoInfoMapper.insert(cargoInfo);
        createReqVO.setId(cargoInfo.getId());
        cargoFileService.createUpdateOrder(createReqVO);

        //添加日志
        CargoRecordDO recordDO = CargoRecordDO.createVo(cargoInfo.getId(), cargoInfo.getStatus(), "新建订单");
        cargoRecordMapper.insert(recordDO);
        // 返回
        return cargoInfo.getId();
    }

    private boolean checkOrderUnique(String mainOrderId, Long id){
        LambdaQueryWrapper<CargoInfoDO> query = Wrappers.lambdaQuery(CargoInfoDO.class)
                .eq(CargoInfoDO::getMainOrderId, mainOrderId);
        if(id != null){
            query.ne(CargoInfoDO::getId, id);
        }
        Long num = cargoInfoMapper.selectCount(query);
        if(num > 0){
            return false;
        }else{
            return true;
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateCargoInfo(CargoInfoSaveReqVO vo) {
        // 校验存在
//        validateCargoInfoExists(updateReqVO.getId());
        CargoInfoDO old = cargoInfoMapper.selectById(vo.getId());
        if(!old.getServiceId().equals(SecurityFrameworkUtils.getLoginUserId())
                && !old.getServiceOtherId().equals(SecurityFrameworkUtils.getLoginUserId())){
            throw exception(CARGO_INFO_UPDATE_ERROR);
        }
        if(old == null){
            throw exception(CARGO_INFO_NOT_EXISTS);
        }
        if(!ValidationRulesUtils.checkTrackingNumber(vo.getMainOrderId())){
            throw exception(CARGO_INFO_ORDER_ERROR);
        }
        if(!checkOrderUnique(vo.getMainOrderId(), vo.getId())){
            throw exception(CARGO_INFO_ORDER_NO_ERROR);
        }
        // 更新
//        CargoInfoDO updateObj = BeanUtils.toBean(updateReqVO, CargoInfoDO.class);
        if(old.getStatus().equals(OrderStatusEnum.FINISHED.getStatus())){
            throw exception(CARGO_INFO_STATUS_ERR);
        }
        LambdaUpdateWrapper<CargoInfoDO> updateWrapper = Wrappers.lambdaUpdate(CargoInfoDO.class)
                .eq(CargoInfoDO::getId, vo.getId())
                .set(CargoInfoDO::getWarehouseNumber, vo.getWarehouseNumber())
                .set(CargoInfoDO::getProductNameMode, vo.getProductNameMode())
                .set(CargoInfoDO::getSpecialProtection, vo.getSpecialProtection())
                .set(CargoInfoDO::getComesDangerousBag, vo.getComesDangerousBag())
                .set(CargoInfoDO::getMainOrderId, vo.getMainOrderId());
        if(old.getStorage() == 1 && vo.getStorage() == 0
                && old.getStatus().equals(OrderStatusEnum.PENDING_ENTRY.getStatus()) ){
            updateWrapper.set(CargoInfoDO::getStatus, OrderStatusEnum.PENDING_CHECK.getStatus())
                    .set(CargoInfoDO::getPlanStatus, PlanStatusEnum.PENDING_PLANING.getStatus());

            //添加日志
            CargoRecordDO recordDO = CargoRecordDO.createVo(vo.getId(), OrderStatusEnum.PENDING_CHECK.getStatus(),
                    "修改订单："+OrderStatusEnum.PENDING_CHECK.getName());
            cargoRecordMapper.insert(recordDO);
        }else if(old.getStorage() == 0 && vo.getStorage() == 1
                && old.getStatus().equals(OrderStatusEnum.PENDING_CHECK.getStatus())
                && old.getPlanStatus().equals(PlanStatusEnum.PENDING_PLANING.getStatus())){
            updateWrapper.set(CargoInfoDO::getStatus, OrderStatusEnum.PENDING_ENTRY.getStatus())
                    .set(CargoInfoDO::getPlanStatus, PlanStatusEnum.NOTHING.getStatus());

            CargoRecordDO recordDO = CargoRecordDO.createVo(vo.getId(), OrderStatusEnum.PENDING_ENTRY.getStatus(),
                    "修改订单："+OrderStatusEnum.PENDING_ENTRY.getName());
            cargoRecordMapper.insert(recordDO);
        }
        updateWrapper.set(CargoInfoDO::getStorage, vo.getStorage())
                .set(CargoInfoDO::getSubOrderId, vo.getSubOrderId())
                .set(CargoInfoDO::getDestinationCityId, vo.getDestinationCityId())
                .set(CargoInfoDO::getSubDestCityId, vo.getSubDestCityId())
                .set(CargoInfoDO::getServiceGrantStatus, vo.getServiceGrantStatus())
                .set(CargoInfoDO::getServiceOtherId, vo.getServiceOtherId())
                .set(CargoInfoDO::getWeight, vo.getWeight())
                .set(CargoInfoDO::getVolume, vo.getVolume())
                .set(CargoInfoDO::getMainCategoryCode, vo.getMainCategoryCode())
                .set(CargoInfoDO::getSubCategoryCode, vo.getSubCategoryCode())
                .set(CargoInfoDO::getNote, vo.getNote())
                .set(CargoInfoDO::getRandomFlag, vo.getRandomFlag())
                .set(CargoInfoDO::getCustomsClearance, vo.getCustomsClearance())
                .set(CargoInfoDO::getTheGround, vo.getTheGround())
                .set(CargoInfoDO::getCompanyId, vo.getCompanyId())
                .set(CargoInfoDO::getMainCustomerId, vo.getMainCustomerId())
                .set(CargoInfoDO::getSubCustomerId1, vo.getSubCustomerId1())
                .set(CargoInfoDO::getSubCustomerId2, vo.getSubCustomerId2())
                .set(CargoInfoDO::getFreight, vo.getFreight())
                .set(CargoInfoDO::getRac, vo.getRac())
                .set(CargoInfoDO::getFlightNumber, vo.getFlightNumber())
                .set(CargoInfoDO::getFlightDate, vo.getFlightDate());
        cargoInfoMapper.update(updateWrapper);

        cargoFileService.createUpdateOrder(vo);


    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void receiptInput(CargoReceiptVO createReqVO){
        CargoInfoDO old = cargoInfoMapper.selectById(createReqVO.getCargoId());
        if(old == null){
            throw exception(CARGO_INFO_NOT_EXISTS);
        }
        if(!old.getStatus().equals(OrderStatusEnum.PENDING_ENTRY.getStatus())){
            throw exception(CARGO_INFO_STATUS_ERR);
        }
        cargoFileService.receiptInput(createReqVO);
        LambdaUpdateWrapper<CargoInfoDO> updateWrapper = Wrappers.lambdaUpdate(CargoInfoDO.class)
                .eq(CargoInfoDO::getId, createReqVO.getCargoId())
                .set(CargoInfoDO::getStatus, OrderStatusEnum.PENDING_PACK.getStatus());
        cargoInfoMapper.update(updateWrapper);

        CargoRecordDO recordDO = CargoRecordDO.createVo(createReqVO.getCargoId(), OrderStatusEnum.PENDING_PACK.getStatus(),
                "收货录入");
        cargoRecordMapper.insert(recordDO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void packGoods(CargoPackVO receiptVO){
        CargoInfoDO old = cargoInfoMapper.selectById(receiptVO.getCargoId());
        if(old == null){
            throw exception(CARGO_INFO_NOT_EXISTS);
        }
        if(!old.getStatus().equals(OrderStatusEnum.PENDING_PACK.getStatus())){
            throw exception(CARGO_INFO_STATUS_ERR);
        }
        cargoFileService.packGoods(receiptVO);

        LambdaUpdateWrapper<CargoInfoDO> updateWrapper = Wrappers.lambdaUpdate(CargoInfoDO.class)
                .eq(CargoInfoDO::getId, receiptVO.getCargoId())
                .set(CargoInfoDO::getStatus, OrderStatusEnum.PENDING_RELEASE.getStatus())
                .set(CargoInfoDO::getPackStatus, PackStatusEnum.PENDING_CHECK.getStatus())
                .set(CargoInfoDO::getPlanStatus, PlanStatusEnum.NOTHING.getStatus())
                .set(CargoInfoDO::getSafetyCheck, 0);
        cargoInfoMapper.update(updateWrapper);

        CargoRecordDO recordDO = CargoRecordDO.createVo(receiptVO.getCargoId(), OrderStatusEnum.PENDING_RELEASE.getStatus(),
                "货物包装");
        cargoRecordMapper.insert(recordDO);
    }

    @Override
    public void packCheck(cargoInfoPackCheckVO vo){
        CargoInfoDO old = cargoInfoMapper.selectById(vo.getId());
        if(old == null){
            throw exception(CARGO_INFO_NOT_EXISTS);
        }
        if(old.getStatus().equals(OrderStatusEnum.PENDING_RELEASE.getStatus())
                && old.getPackStatus().equals(PackStatusEnum.PENDING_CHECK.getStatus())){
            LambdaUpdateWrapper<CargoInfoDO> updateWrapper = Wrappers.lambdaUpdate(CargoInfoDO.class)
                    .eq(CargoInfoDO::getId, vo.getId())
                    .set(CargoInfoDO::getPackStatus, vo.getPackStatus());
            if(vo.getPackStatus().equals(PackStatusEnum.SUCCESS.getStatus())){
                updateWrapper.set(CargoInfoDO::getPlanStatus, PlanStatusEnum.PENDING_PLANING.getStatus());
            }else if(vo.getPackStatus().equals(PackStatusEnum.FAIL.getStatus())){
                updateWrapper.set(CargoInfoDO::getStatus, OrderStatusEnum.PENDING_PACK.getStatus());
            }
            cargoInfoMapper.update(updateWrapper);
        }else{
            throw exception(CARGO_INFO_STATUS_ERR);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addPlan(CargoInfoAddPlanVO vo){
        LambdaUpdateWrapper<CargoInfoDO> updateWrapper = Wrappers.lambdaUpdate(CargoInfoDO.class)
                .in(CargoInfoDO::getId, vo.getIds())
                .eq(CargoInfoDO::getPlanStatus, PlanStatusEnum.PENDING_PLANING.getStatus())
                .set(CargoInfoDO::getPlanStatus, PlanStatusEnum.PLANING.getStatus())
                .set(CargoInfoDO::getSafetyCheck, 0)
                .set(CargoInfoDO::getPlanTime, LocalDateTime.now());
        int rows = cargoInfoMapper.update(updateWrapper);
        if(rows != vo.getIds().size()){
            throw exception(CARGO_INFO_ADD_PAN_ERR);
        }

        CargoPlanDO planDO = new CargoPlanDO();
        planDO.setPlanTime(LocalDateTime.now());
        planDO.setCargoIds(String.join(",", vo.getIds().stream().map(it->it.toString()).collect(Collectors.toList())));
        cargoPlanService.addPlan(planDO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void shipmentGoods(CargoShipmentVO cargoShipmentVO){
        CargoInfoDO old = cargoInfoMapper.selectById(cargoShipmentVO.getCargoId());
        if(old == null){
            throw exception(CARGO_INFO_NOT_EXISTS);
        }
        if(old.getStatus().equals(OrderStatusEnum.PENDING_RELEASE.getStatus())
            && old.getPackStatus().equals(PackStatusEnum.SUCCESS.getStatus())
            && old.getPlanStatus().equals(PlanStatusEnum.PLANING.getStatus())){
//            cargoFileService.shipmentGoods(cargoShipmentVO);

            LambdaUpdateWrapper<CargoInfoDO> updateWrapper = Wrappers.lambdaUpdate(CargoInfoDO.class)
                    .eq(CargoInfoDO::getId, cargoShipmentVO.getCargoId())
                    .set(CargoInfoDO::getStatus, OrderStatusEnum.PENDING_CHECK.getStatus());
            cargoInfoMapper.update(updateWrapper);

            CargoRecordDO recordDO = CargoRecordDO.createVo(cargoShipmentVO.getCargoId(), OrderStatusEnum.PENDING_CHECK.getStatus(),
                    "仓库出货");
            cargoRecordMapper.insert(recordDO);
        }else{
            throw exception(CARGO_INFO_STATUS_ERR);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void SceneGoods(CargoShipmentVO cargoShipmentVO){
        CargoInfoDO old = cargoInfoMapper.selectById(cargoShipmentVO.getCargoId());
        if(old == null){
            throw exception(CARGO_INFO_NOT_EXISTS);
        }
        if(old.getStatus().equals(OrderStatusEnum.PENDING_CHECK.getStatus())){
            if(!old.getPlanStatus().equals(PlanStatusEnum.PLANING.getStatus())){
                throw exception(CARGO_INFO_NO_PLAN);
            }
            cargoFileService.shipmentGoods(cargoShipmentVO);

            LambdaUpdateWrapper<CargoInfoDO> updateWrapper = Wrappers.lambdaUpdate(CargoInfoDO.class)
                    .eq(CargoInfoDO::getId, cargoShipmentVO.getCargoId())
                    .set(CargoInfoDO::getStatus, OrderStatusEnum.PENDING_CUSTOMS.getStatus());
            cargoInfoMapper.update(updateWrapper);

            CargoRecordDO recordDO = CargoRecordDO.createVo(cargoShipmentVO.getCargoId(), OrderStatusEnum.PENDING_CUSTOMS.getStatus(),
                    "安检");
            cargoRecordMapper.insert(recordDO);
        }else{
            throw exception(CARGO_INFO_STATUS_ERR);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addCustoms(CargoCustomsVO cargoCustomsVO){
        CargoInfoDO old = cargoInfoMapper.selectById(cargoCustomsVO.getCargoId());
        if(old == null){
            throw exception(CARGO_INFO_NOT_EXISTS);
        }
        if(!old.getStatus().equals(OrderStatusEnum.PENDING_CUSTOMS.getStatus())){
            throw exception(CARGO_INFO_STATUS_ERR);
        }
        if(cargoCustomsVO.getStatus() == 1){
            LambdaUpdateWrapper<CargoInfoDO> updateWrapper = Wrappers.lambdaUpdate(CargoInfoDO.class)
                    .eq(CargoInfoDO::getId, cargoCustomsVO.getCargoId())
                    .set(CargoInfoDO::getStatus, OrderStatusEnum.PENDING_SUBMISSION.getStatus())
                    .set(CargoInfoDO::getSafetyCheck, 1);
            cargoInfoMapper.update(updateWrapper);

            CargoRecordDO recordDO = CargoRecordDO.createVo(cargoCustomsVO.getCargoId(), OrderStatusEnum.PENDING_SUBMISSION.getStatus(),
                    "报关成功");
            cargoRecordMapper.insert(recordDO);
        }else if(cargoCustomsVO.getStatus() == -1){
            LambdaUpdateWrapper<CargoInfoDO> updateWrapper = Wrappers.lambdaUpdate(CargoInfoDO.class)
                    .eq(CargoInfoDO::getId, cargoCustomsVO.getCargoId())
                    .set(CargoInfoDO::getSafetyCheck, -1);
            if(old.getStorage() == 1){
                updateWrapper.set(CargoInfoDO::getStatus, OrderStatusEnum.PENDING_PACK.getStatus())
                .set(CargoInfoDO::getPackStatus, PackStatusEnum.PENDING_CHECK.getStatus())
                .set(CargoInfoDO::getPlanStatus, PlanStatusEnum.NOTHING.getStatus());
            }else{
                updateWrapper.set(CargoInfoDO::getStatus, OrderStatusEnum.PENDING_CHECK.getStatus())
                        .set(CargoInfoDO::getPlanStatus, PlanStatusEnum.PENDING_PLANING.getStatus());
            }
            cargoInfoMapper.update(updateWrapper);

            CargoRecordDO recordDO = CargoRecordDO.createVo(cargoCustomsVO.getCargoId(), OrderStatusEnum.PENDING_SUBMISSION.getStatus(),
                    "报关退回");
            cargoRecordMapper.insert(recordDO);

            //计划删除
            CargoPlanDO planDO = cargoPlanService.getByCargoId(cargoCustomsVO.getCargoId());
            CancelPlanVO cancelPlanVO = new CancelPlanVO();
            cancelPlanVO.setCargoIds(Lists.newArrayList(cargoCustomsVO.getCargoId()));
            cancelPlanVO.setId(planDO.getId());
            cargoPlanService.backPlan(cancelPlanVO);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addHandUp(CargoShipmentVO cargoShipmentVO){
        CargoInfoDO old = cargoInfoMapper.selectById(cargoShipmentVO.getCargoId());
        if(old == null){
            throw exception(CARGO_INFO_NOT_EXISTS);
        }
        if(old.getStatus().equals(OrderStatusEnum.PENDING_SUBMISSION.getStatus())){
            if(!old.getPlanStatus().equals(PlanStatusEnum.PLANING.getStatus())){
                throw exception(CARGO_INFO_NO_PLAN);
            }

            LambdaUpdateWrapper<CargoInfoDO> updateWrapper = Wrappers.lambdaUpdate(CargoInfoDO.class)
                    .eq(CargoInfoDO::getId, cargoShipmentVO.getCargoId())
                    .set(CargoInfoDO::getPlanStatus, PlanStatusEnum.FINISH.getStatus())
                    .set(CargoInfoDO::getStatus, OrderStatusEnum.TRANSIT.getStatus());
            cargoInfoMapper.update(updateWrapper);

            CargoRecordDO recordDO = CargoRecordDO.createVo(cargoShipmentVO.getCargoId(), OrderStatusEnum.TRANSIT.getStatus(),
                    "交单");
            cargoRecordMapper.insert(recordDO);
        }else{
            throw exception(CARGO_INFO_STATUS_ERR);
        }
    }

    @Override
    public void addAirway(CargoAirwayVO vo){
        CargoInfoDO old = cargoInfoMapper.selectById(vo.getId());
        if(old == null){
            throw exception(CARGO_INFO_NOT_EXISTS);
        }
        if(old.getStatus().equals(OrderStatusEnum.TRANSIT.getStatus())){
            LambdaUpdateWrapper<CargoInfoDO> updateWrapper = Wrappers.lambdaUpdate(CargoInfoDO.class)
                    .eq(CargoInfoDO::getId, vo.getId())
                    .set(CargoInfoDO::getAirway, vo.getAirway())
                    .set(CargoInfoDO::getFlightNumber, vo.getFlightNumber())
                    .set(CargoInfoDO::getTrendsInfo, vo.getTrendsInfo());
            if(vo.getStatus() == 1){
                updateWrapper.set(CargoInfoDO::getStatus, OrderStatusEnum.FINISHED.getStatus());
            }
            cargoInfoMapper.update(updateWrapper);

            CargoRecordDO recordDO = CargoRecordDO.createVo(vo.getId(), OrderStatusEnum.FINISHED.getStatus(),
                    "订单完成");
            cargoRecordMapper.insert(recordDO);
        }else{
            throw exception(CARGO_INFO_STATUS_ERR);
        }
    }

    @Override
    public void arrival(CargoArrivalVO cargoArrivalVO){
//        CargoInfoDO old = cargoInfoMapper.selectById(cargoArrivalVO.getCargoId());
//        if(old == null){
//            throw exception(CARGO_INFO_NOT_EXISTS);
//        }
//        if(!old.getStatus().equals(OrderStatusEnum.PENDING_FINISH.getStatus())){
//            throw exception(CARGO_INFO_STATUS_ERR);
//        }
//        if(!cargoArrivalVO.getStatus().equals(OrderStatusEnum.PENDING_FINISH.getStatus())
//                && !cargoArrivalVO.getStatus().equals(OrderStatusEnum.FINISHED.getStatus())){
//            throw exception(CARGO_INFO_STATUS_ERR);
//        }
//        LambdaUpdateWrapper<CargoInfoDO> updateWrapper = Wrappers.lambdaUpdate(CargoInfoDO.class)
//                .eq(CargoInfoDO::getId, cargoArrivalVO.getCargoId());
//        updateWrapper.set(CargoInfoDO::getReminder, cargoArrivalVO.getReminder());
//        if(cargoArrivalVO.getStatus().equals(OrderStatusEnum.FINISHED.getStatus())){
//            updateWrapper.set(CargoInfoDO::getArrivalTime, LocalDateTime.now());
//            updateWrapper.set(CargoInfoDO::getStatus, OrderStatusEnum.FINISHED.getStatus());
//        }
//        cargoInfoMapper.update(updateWrapper);
    }

    @Override
    public void shippedBack(ShippedBackVO shippedBackVO){
//        CargoInfoDO old = cargoInfoMapper.selectById(shippedBackVO.getCargoId());
//        if(old == null){
//            throw exception(CARGO_INFO_NOT_EXISTS);
//        }
//        if(!old.getStatus().equals(OrderStatusEnum.TO_BE_SHIPPED.getStatus())){
//            throw exception(CARGO_INFO_STATUS_ERR);
//        }
//        LambdaUpdateWrapper<CargoInfoDO> updateWrapper = Wrappers.lambdaUpdate(CargoInfoDO.class)
//                .eq(CargoInfoDO::getId, shippedBackVO.getCargoId())
//                .set(CargoInfoDO::getRepulseStatus, 1)
//                .set(CargoInfoDO::getRepulseTime, LocalDateTime.now())
//                .set(CargoInfoDO::getRemark, shippedBackVO.getRemark())
//                .set(CargoInfoDO::getStatus, OrderStatusEnum.TO_BE_RECEIPT.getStatus());
//        cargoInfoMapper.update(updateWrapper);
    }

    @Override
    public void deleteCargoInfo(Long id) {
        // 校验存在
        validateCargoInfoExists(id);
        // 删除
        cargoInfoMapper.deleteById(id);
    }

    private void validateCargoInfoExists(Long id) {
        if (cargoInfoMapper.selectById(id) == null) {
            throw exception(CARGO_INFO_NOT_EXISTS);
        }
    }

    @Override
    public CargoInfoRespVO getCargoInfo(Long id) {
        return cargoInfoMapper.getInfoById(id);
    }

    @Override
    public CargoInfoDO getById(Long id){
        return cargoInfoMapper.selectById(id);
    }

    @Override
    public PageResult<CargoInfoRespVO> getCargoInfoPage(CargoInfoPageReqVO pageReqVO) {
        Page<CargoInfoPageReqVO> mpPage = MyBatisUtils.buildPage(pageReqVO);
        List<CargoInfoRespVO> list = cargoInfoMapper.pageCargoInfos(mpPage, pageReqVO);
        return new PageResult<>(list, mpPage.getTotal());
    }

    @Override
    public List<CargoInfoSumaryVO> sumaryOrderStatus(CargoInfoPageReqVO reqVO){
        return cargoInfoMapper.sumaryOrderStatus(reqVO);
    }

}